package com.meiyu.live.tv.widget;

import android.animation.TimeInterpolator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Animatable;
import android.os.SystemClock;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Choreographer;
import android.view.View;
import android.view.Choreographer.FrameCallback;
import android.view.animation.LinearInterpolator;

import java.util.ArrayDeque;
import java.util.Iterator;
import java.util.Queue;

public class PopAnimateView extends View implements Animatable, FrameCallback {
    private static final String TAG = "PopAnimateView";
    private static boolean DBG = false;
    final Choreographer choreographer;
    boolean mIsRunning;
    Adapter mAdapter;
    Observer mObserver;
    int mItemCount;
    long mFrameTimeMillis;
    long mCurrentTimeMillis;
    boolean mStateIsRunning;
    private TimeInterpolator mInterpolator;

    public PopAnimateView(Context context) {
        this(context, (AttributeSet) null);
    }

    public PopAnimateView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public PopAnimateView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.choreographer = Choreographer.getInstance();
        this.mStateIsRunning = false;
        this.mInterpolator = new LinearInterpolator();
        this.mObserver = new Observer() {
            public void onItemInserted(PopHolder holder) {
                if (PopAnimateView.this.mItemCount++ == 0) {
                    PopAnimateView.this.start();
                }

                PopAnimateView.logd("inserted count=%s", new Object[]{Integer.valueOf(PopAnimateView.this.mItemCount)});
            }

            public void onItemRemoved(PopHolder holder) {
                if (--PopAnimateView.this.mItemCount == 0) {
                    PopAnimateView.this.stop();
                }

                PopAnimateView.logd("removed count=%s", new Object[]{Integer.valueOf(PopAnimateView.this.mItemCount)});
            }
        };
    }

    public void setAdapter(Adapter adapter) {
        if (this.mAdapter != adapter) {
            if (this.mAdapter != null) {
                this.mAdapter.unregisterObserver(this.mObserver);
                this.mAdapter.clear();
                this.mItemCount = 0;
                this.stop();
            }

            this.mAdapter = adapter;
            if (adapter != null) {
                adapter.registerObserver(this.mObserver);
            }

        }
    }

    protected void onDraw(Canvas canvas) {
        if (this.mIsRunning && this.mAdapter != null) {
            this.mAdapter.drawAllItems(this, canvas);
        }

    }

    public void setInterpolator(TimeInterpolator interpolator) {
        this.mInterpolator = interpolator;
    }

    public <T extends PopHolder> T obtain(long duration) {
        Adapter adapter = this.mAdapter;
        return adapter != null ? (T) adapter.obtain(duration, this.mFrameTimeMillis) : null;
    }

    public void doFrame(long frameTimeNanos) {
        if (this.mIsRunning) {
            this.mFrameTimeMillis = frameTimeNanos / 1000000L - this.mCurrentTimeMillis;
            this.invalidate();
            this.choreographer.postFrameCallback(this);
        }

    }

    public void start() {
        if (!this.mIsRunning) {
            this.choreographer.removeFrameCallback(this);
            this.choreographer.postFrameCallback(this);
            this.mIsRunning = true;
            this.mCurrentTimeMillis = SystemClock.uptimeMillis();
            logd("started", new Object[0]);
        }

    }

    public void stop() {
        if (this.mIsRunning) {
            this.choreographer.removeFrameCallback(this);
            this.mIsRunning = false;
            this.mFrameTimeMillis = 0L;
            logd("stopped", new Object[0]);
        }

    }

    public void pause() {
        this.mStateIsRunning = this.mIsRunning;
        this.stop();
    }

    public void resume() {
        if (this.mStateIsRunning) {
            this.start();
        }

        this.mStateIsRunning = false;
    }

    public boolean isRunning() {
        return this.mIsRunning;
    }

    private static void logd(String msg, Object... args) {
        if (DBG) {
            Log.d("PopAnimateView", String.format(msg, args));
        }

    }

    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        this.resume();
    }

    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        this.pause();
    }

    public abstract static class Adapter<T extends PopHolder> {
        private Queue<T> recycler = new ArrayDeque();
        private Queue<T> interbox = new ArrayDeque();
        private Observer mObserver;

        public Adapter() {
        }

        public Adapter(int capacity) {
            for (int i = 0; i < capacity; ++i) {
                this.recycler.offer(this.obtain());
            }
        }

        void registerObserver(Observer observer) {
            this.mObserver = observer;
        }

        void unregisterObserver(Observer observer) {
            this.mObserver = null;
        }

        private T obtain() {
            T item = this.recycler.poll();
            if (item == null) {
                item = this.onCreatePopHolder();
                item.mAdapter = (Adapter<PopHolder>) this;
            }

            return item;
        }

        T obtain(long duration, long frameTimeMillis) {
            T item = this.obtain();
            item.reset(duration, frameTimeMillis);
            return item;
        }

        void drawAllItems(PopAnimateView view, Canvas canvas) {
            Iterator<T> iterator = this.interbox.iterator();

            while (iterator.hasNext()) {
                T item = iterator.next();
                long timeDiff = view.mFrameTimeMillis - item.mEnterTime;
                int saveCount = canvas.save();
                this.onDrawItem(item, canvas, view.mInterpolator.getInterpolation((float) timeDiff / (float) item.mDuration));
                canvas.restoreToCount(saveCount);
                if (timeDiff >= item.mDuration) {
                    iterator.remove();
                    item.recycle();
                }
            }

        }

        void notifyItemInserted(PopHolder holder) {
            if (this.mObserver != null) {
                this.mObserver.onItemInserted(holder);
            }

        }

        void notifyItemRemoved(PopHolder holder) {
            if (this.mObserver != null) {
                this.mObserver.onItemRemoved(holder);
            }

        }

        void clear() {
            this.mObserver = null;
            this.recycler.clear();
            this.interbox.clear();
        }

        protected abstract void onDrawItem(T popHolder, Canvas canvas, float interpolationValue);

        protected abstract T onCreatePopHolder();
    }

    interface Observer {
        void onItemInserted(PopHolder var1);

        void onItemRemoved(PopHolder var1);
    }

    public abstract static class PopHolder {
        long mDuration;
        long mEnterTime;
        Adapter<PopHolder> mAdapter;

        protected PopHolder() {
        }

        public long getDuration() {
            return this.mDuration;
        }

        protected void reset(long duration, long enterTimeMillis) {
            this.mDuration = duration;
            this.mEnterTime = enterTimeMillis;
        }

        public boolean insert() {
            boolean inserted = this.mAdapter.interbox.offer(this);
            if (inserted) {
                this.mAdapter.notifyItemInserted(this);
            }

            return inserted;
        }

        public void recycle() {
            this.mAdapter.recycler.offer(this);
            this.mAdapter.notifyItemRemoved(this);
        }
    }
}
